*********************************************************************
* This code replicates all of the results in Hinston and Schultz,
* "Closing Pandora's Box: Can Shared Vulnerability Underpin Territorial 
* Stability?" International Organization
*
* Note: This code requires installation of the following ado file:
*	dcr.ado (available from https://github.com/jscarlson/stata-dcr)
*
* Note: In order to collect results on various subets of data, this code
* creates temporary files called results.dta or results0.dta, etc.
*********************************************************************

graph set window fontface "default"
set scheme s1mono


**************************************************************
* Results reported in  paper
**************************************************************

**************************************
* Figure 1 - Claim frequency by Region
**************************************

* create file to store results
clear
set obs 8
gen reg=.
gen dispute_rate=.
label var reg "Region"
label var dispute_rate "Share of Borders Disputed"
save results, replace

* loop through regions

local i=1
forvalues r=0/7 {
	
	use pandorasdata, clear
	keep if r`r'==1
	
	* is there ever a claim
	egen everdisputed=max(claim_onset), by(dyad)
	
	* reduce to one obs per dyad
	duplicates drop dyad, force
	
	* get rate per dyad
	qui sum everdisputed
	
	* store results
	use results, clear
	qui replace reg=`r' in `i'
	qui replace dispute_rate=r(mean) in `i'
	save results, replace
	
	local i=`i'+1
	
	}
	
graph bar dispute_rate, over(reg, relabel(1 "All" 2 "Africa" 3 "Americas" 4 "Asia" 5 "MENA" 6 "Europe" 7 `" "Europe" "pre-47" "' 8 `" "Europe" "post-47" "')) ytitle("Proportion of Borders Disputed") legend(off)


****************************************************
* Figure 2 - Mean levels of vulnerability by region 
****************************************************

* this local lists the measures of vulnerability to summarize
local varlist "thirdp_border_wt thirdp_losstek_unclaimed_wt nhood_losstek_unclaimed"

* create file to store results
clear
set obs 7
gen reg=.

foreach v of local varlist {

	gen `v'_rate=.

	}

label var reg "Region"
save results, replace

* loop through regions

local i=1
forvalues r=1/7 {
	
	* loop through vulnerability measures
	foreach v of local varlist {

		
	use pandorasdata, clear
	keep if r`r'==1

	* sub in hegtek data for europe when split (r= 5, 6, or 7)
	
	if (`r'==5|`r'==6|`r'==7) {
	
		qui replace thirdp_losstek_unclaimed_wt = thirdp_losshegtek_unclaimed_wt
		qui replace nhood_losstek_unclaimed = nhood_losshegtek_unclaimed
		
		}
		
		summ `v'
		local m=r(mean)
		
		* store results
		use results, clear
		qui replace reg=`r' in `i'
		qui replace `v'_rate=`m' in `i'
		save results, replace
		
		}

		local i=`i'+1

	}

use results,clear

*create sub-graphs
graph bar thirdp_border_wt_rate, over(reg, relabel(1 "Africa" 2 "Americas" 3 "Asia" 4 "MENA" 5 "Europe" 6 `" "Europe" "pre-47" "' 7 `" "Europe" "post-47" "')) ytitle("Number of Third Parties" "(Weighted)") legend(off) name(mean1, replace)

* drop Americas from these sub-graphs
drop if reg==2
graph bar thirdp_losstek_unclaimed_wt_rate, over(reg, relabel(1 "Africa" 2 "Asia" 3 "MENA" 4 "Europe" 5 `" "Europe" "pre-47" "' 6 `" "Europe" "post-47" "')) ytitle("Third Parties with Potential Claims" "(Weighted)") legend(off) name(mean2, replace)
graph bar nhood_losstek_unclaimed_rate, over(reg, relabel(1 "Africa" 2 "Asia" 3 "MENA" 4 "Europe" 5 `" "Europe" "pre-47" "' 6 `" "Europe" "post-47" "')) ytitle("Dyads with Potential Claims in Neighborhood") legend(off) name(mean3, replace)

graph combine mean2 mean3, cols(2) name(mean23, replace)
graph combine mean1 mean23, rows(2)



*************************************************
* Third party effects regressions
*************************************************


* Need this program to substitute DCRSEs into stored estimates

capture program drop switch

program switch, eclass

	ereturn repost V=V

end

* loop over regions (skipping Americas r=2)

foreach r in 0 1 3 4 5 6 7 {

	* create files to store results for each region/subsample
	clear
	set obs 7
	
	forvalues i=0/2 {
	qui gen bhat`i'=. 
	qui gen bhat`i'_up=.
	qui gen bhat`i'_lo=.
		}	
	
	qui gen indicator=.

	label var bhat0 "Direct effect"
	label var bhat1 "Deterrent effect (count)"
	label var bhat2 "Deterrent effect (weighted)"
	
	save results`r', replace

	* Number of borders 

	local indicator="border"
	local i=1

	use pandorasdata, clear
	keep if r`r'==1
	
	*recale dv
	qui replace claim_onset=claim_onset*100
	
	* unweighted measure
	local iv "thirdp_border reverse_claim_ongoing p_chal energy1 dem1_imp dem2_imp bothdem_imp ts1 ts2 ts3"
	
	* use regional fixed effects in full sample
	local regfe ""
	if `r'==0 {
	
		local regfe "america asia mena europe"
		
		}
	
	* estimate regression with DCRSEs
	qui dcr reg claim_onset `iv' `regfe', dm1(chal) dm2(tgt) dofundo(asylike) psd dofcorr 
	mat b1=e(b)
	mat V1=e(V)
	mat V=e(V)
	local dof=e(N_unique_dm)-1
	
	* trick Stata into re-creating the estimates using the DCRSEs
	qui reg claim_onset `iv' `regfe', dof(`dof')
	switch
	estimates store `indicator'`r'

	* weighted measure
	local iv "thirdp_border_wt reverse_claim_ongoing p_chal energy1 dem1_imp dem2_imp bothdem_imp ts1 ts2 ts3"
	
	* estimate regression with DCRSEs
	qui dcr reg claim_onset `iv' `regfe', dm1(chal) dm2(tgt) dofundo(asylike) psd dofcorr 
	mat b2=e(b)
	mat V2=e(V)
	mat V=e(V)
	local dof=e(N_unique_dm)-1
	
	* trick Stata into re-creating the estimates using the DCRSEs
	qui reg claim_onset `iv' `regfe', dof(`dof')
	switch
	estimates store `indicator'`r'wt

	* save results
	qui use results`r', clear
	qui replace indicator=`i' in `i'
	qui replace bhat1=b1[1,1] in `i'
	qui replace bhat1_up=b1[1,1]+invttail(`dof',.025)*sqrt(V1[1,1]) in `i'
	qui replace bhat1_lo=b1[1,1]-invttail(`dof',.025)*sqrt(V1[1,1]) in `i'
	
	qui replace bhat2=b2[1,1] in `i'
	qui replace bhat2_up=b2[1,1]+invttail(`dof',.025)*sqrt(V2[1,1]) in `i'
	qui replace bhat2_lo=b2[1,1]-invttail(`dof',.025)*sqrt(V2[1,1]) in `i'
	qui save results`r', replace

	local i=`i'+1
	
	* Loop through other indicators of vulnerability
	
	foreach indicator in tek loss losstek {
				
		use pandorasdata, clear
		*recale dv
		qui replace claim_onset=claim_onset*100
		
		
		qui keep if r`r'==1
		
		* drop Americas from full sample tests using these indicators
		qui drop if america==1&`r'==0
		

		* sub in hegtek data for Europe sub-samples
		
		if (`r'==5|`r'==6|`r'==7) {
		
			qui replace tek=hegtek
			qui replace losstek=losshegtek
			qui replace tek_unclaimed=hegtek_unclaimed
			qui replace losstek_unclaimed=losshegtek_unclaimed
			qui replace reverse_tek_unclaimed=reverse_hegtek_unclaimed
			qui replace reverse_losstek_unclaimed=reverse_losshegtek_unclaimed
			
			foreach v in unclaimed claimed unclaimed_wt claimed_wt {
			
				qui replace thirdp_tek_`v'=thirdp_hegtek_`v'
				qui replace thirdp_losstek_`v'=thirdp_losshegtek_`v'

				}
				
			}
		
		* whenever losses in the indicator, restrict to cases pre-1st claim in dyad
		if "`indicator'"=="loss"|"`indicator'"=="losstek" {
		
			qui drop if post_1st_claim==1
			
			}
		
		* unweighted indicators
		local iv "`indicator'_unclaimed thirdp_`indicator'_unclaimed thirdp_`indicator'_claimed  reverse_`indicator'_unclaimed reverse_claim_ongoing p_chal energy1 dem1_imp dem2_imp bothdem_imp ts1 ts2 ts3"
		
		* use regional fixed effects in full sample
		local regfe ""
		
		if `r'==0 {
		
			local regfe "asia mena europe"
			
			}
		
		* estimate regression with DCRSEs
		dcr reg claim_onset `iv' `regfe', dm1(chal) dm2(tgt) dofundo(asylike) psd dofcorr 
		mat b1=e(b)
		mat V1=e(V)
		mat V=e(V)
		local dof=e(N_unique_dm)-1
		
		* this code is needed to store the estimates with DCRSEs for later output
		qui reg claim_onset `iv' `regfe', dof(`dof')
		switch
		estimates store `indicator'`r'
		
		* weighted indicators
		local iv "`indicator'_unclaimed thirdp_`indicator'_unclaimed_wt thirdp_`indicator'_claimed_wt reverse_`indicator'_unclaimed reverse_claim_ongoing p_chal energy1 dem1_imp dem2_imp bothdem_imp ts1 ts2 ts3"
	
		dcr reg claim_onset `iv' `regfe', dm1(chal) dm2(tgt) dofundo(asylike) psd dofcorr 
		mat b2=e(b)
		mat V2=e(V)
		mat V=e(V)
		local dof=e(N_unique_dm)-1
	
		* this code is needed to store the estimates with DCRSEs for later output	
		qui reg claim_onset `iv' `regfe', dof(`dof')
		switch
		estimates store `indicator'`r'wt
		
		* store estimates and 95 percent confidence intervals
		qui use results`r', clear
		qui replace indicator=`i' in `i'
		qui replace bhat0=b1[1,1] in `i'
		qui replace bhat0_up=b1[1,1]+invttail(`dof',.025)*sqrt(V1[1,1]) in `i'
		qui replace bhat0_lo=b1[1,1]-invttail(`dof',.025)*sqrt(V1[1,1]) in `i'
		qui replace bhat1=b1[1,2] in `i'
		qui replace bhat1_up=b1[1,2]+invttail(`dof',.025)*sqrt(V1[2,2]) in `i'
		qui replace bhat1_lo=b1[1,2]-invttail(`dof',.025)*sqrt(V1[2,2]) in `i'
		qui replace bhat2=b2[1,2] in `i'
		qui replace bhat2_up=b2[1,2]+invttail(`dof',.025)*sqrt(V2[2,2]) in `i'
		qui replace bhat2_lo=b2[1,2]-invttail(`dof',.025)*sqrt(V2[2,2]) in `i'
		
		qui save results`r', replace
		
		local i=`i'+1
		
		}

	}
	
* Make graphs

foreach r in 0 1 3 4 5 6 7 {

	if `r'==0 {	 
		local region "All"
		}
	if `r'==1 {
		local region "Africa"
		}
	if `r'==3 {
		local region "Asia"
		}
	if `r'==4 {
		local region "MENA"
		}
	if `r'==5 {
		local region "Europe"
		}
	if `r'==6 {
		local region "Europe Pre-1947"
		}
	if `r'==7 { 
		local region "Europe Post-1947"
		}
		
	use results`r', clear

	* this creates offsets on the x-axis to space things nicely
	qui gen ind0=indicator-.2
	qui gen ind1=indicator
	qui gen ind2=indicator+.2 

	* make graph
	
	if `r'< 7 {
		
		twoway (scatter bhat0 ind0, msize(vsmall) msymbol(circle) mcolor(gs8)) (rcap bhat0_up bhat0_lo ind0, lcolor(gs8)) ///
			(scatter bhat1 ind1, msize(vsmall) mcolor(gs1) msymbol(square)) (rcap bhat1_up bhat1_lo ind1, lcolor(gs1))  ///
			(scatter bhat2 ind2, msize(vsmall) msymbol(diamond) mcolor(gs1))  (rcap bhat2_up bhat2_lo ind2, lcolor(gs1)),  ///
			legend(off) xtitle(" ") xscale(range(0.5 4.5)) xlabel(1 "Borders" 2 "TEK" 3 "Loss" 4 `" "Loss or" "TEK""',labsize(small)) ///
			yline(0) ytitle("Marginal Effect") title(`region') name(graph3d`r',replace) nodraw

		}
		
		* add legend to last subgraph
		else {
		
		twoway (scatter bhat0 ind0, msize(vsmall) msymbol(circle) mcolor(gs8)) (rcap bhat0_up bhat0_lo ind0, lcolor(gs8)) ///
			(scatter bhat1 ind1, msize(vsmall) mcolor(gs1) msymbol(square)) (rcap bhat1_up bhat1_lo ind1, lcolor(gs1))  ///
			(scatter bhat2 ind2, msize(vsmall) msymbol(diamond) mcolor(gs1))  (rcap bhat2_up bhat2_lo ind2, lcolor(gs1)),  ///
			legend(order(1 3 5) ring(0) pos(3) xoffset(45) cols(1) region(lstyle(none))) xtitle(" ") xscale(range(0.5 4.5)) xlabel(1 "Borders" 2 "TEK" 3 "Loss" 4 `" "Loss or" "TEK""',labsize(small)) ///
			yline(0) ytitle("Marginal Effect") title(`region') name(graph3d`r',replace) nodraw

		}
		
	}
	

* Create figure 3
graph combine graph3d0 graph3d1 graph3d3 graph3d4 graph3d5 graph3d6 graph3d7, ysize(20) xsize(13) cols(2) 


***************************************************************
* This code creates tables underlying Figure 3 (see Appendix C)
***************************************************************

* this code uses estimates stored above

clear
use pandorasdata

foreach indicator in border tek loss losstek {

	if "`indicator'"=="border" {
	
		local ind "Borders"
		
		}

	if "`indicator'"=="tek" {
	
		local ind "TEK"
		
		}	
	if "`indicator'"=="loss" {
	
		local ind "Loss"
		
		}
	if "`indicator'"=="losstek" {
	
		local ind "Loss or TEK"
		
		}
		
	estout `indicator'0 `indicator'1 `indicator'3 `indicator'4 `indicator'5 `indicator'6 `indicator'7, ///
		cells(b(star fmt(2)) se(par fmt(2))) stats(r2 N, fmt(3 0) labels("\$R^2\$" "Observations")) label varlabels (_cons Constant) ///
		mgroups("All" "Africa" "Asia" "MENA" "Europe" "Europe" "Europe", pattern(1 1 1 1 1 1 1)) ///
		starlevels(* 0.05 ** 0.01) mlabels("" "" "" "" "" "Pre-1947" "Post-1947") collabels(none) noabbrev noomitted style(tex) msign(--) ///
		prehead("\begin{table}\caption{Potential Claims Indicator: `ind' (Count)}" "\footnotesize" "\begin{center}" "\begin{tabular}{l*{@M}{c}}" "\\[-1.8ex]\hline" "\hline \\[-1.8ex]") posthead("\\[-1.8ex] \hline" "\\[-1.8ex]") ///
		prefoot("\\[-1.8ex] \hline") postfoot("\hline" "\hline \\[-1.8ex]" "$^{*} p<0.05, ^{**} p<0.01$" "\end{tabular}" "\end{center}" "\end{table}")
	
	estout `indicator'0wt `indicator'1wt `indicator'3wt `indicator'4wt `indicator'5wt `indicator'6wt `indicator'7wt, ///
		cells(b(star fmt(2)) se(par fmt(2))) stats(r2 N, fmt(3 0) labels("\$R^2\$" "Observations")) label varlabels (_cons Constant) ///
		mgroups("All" "Africa" "Asia" "MENA" "Europe" "Europe" "Europe", pattern(1 1 1 1 1 1 1)) ///
		starlevels(* 0.05 ** 0.01) mlabels("" "" "" "" "" "Pre-1947" "Post-1947") collabels(none) noabbrev noomitted style(tex) msign(--) ///
		prehead("\begin{table}\caption{Potential Claims Indicator: `ind' (Weighted)}" "\footnotesize" "\begin{center}" "\begin{tabular}{l*{@M}{c}}" "\\[-1.8ex]\hline" "\hline \\[-1.8ex]") posthead("\\[-1.8ex] \hline" "\\[-1.8ex]") ///
		prefoot("\\[-1.8ex] \hline") postfoot("\hline" "\hline \\[-1.8ex]" "$^{*} p<0.05, ^{**} p<0.01$" "\end{tabular}" "\end{center}" "\end{table}")
	
	}


********************************************************************************
* Figure 4 - Effect of Potential Claims in the Neighborhoos on Claim Initiation
********************************************************************************

* create file to store results
clear
set obs 10
forvalues i=1/3 {

	qui gen bhat`i'=. 
	qui gen bhat`i'_up=.
	qui gen bhat`i'_lo=.
	
	}

gen reg=.

label var bhat1 "TEK"
label var bhat2 "Loss"
label var bhat3 "Loss or TEK"

save results, replace

* Loop through regions

local i=1

foreach r in 0 1 3 4 5 6 7 {

	local k=1

	foreach indicator in tek loss losstek {

		use pandorasdata, clear
		
		* rescale dv
		replace claim_onset=claim_onset*100
		
		qui keep if r`r'==1&america==0
	
		* sub in hegtek data for europe
		
		if (`r'==5|`r'==6|`r'==7) {
		
			qui replace tek=hegtek
			qui replace losstek=losshegtek
			qui replace tek_unclaimed=hegtek_unclaimed
			qui replace losstek_unclaimed=losshegtek_unclaimed
			qui replace reverse_tek_unclaimed=reverse_hegtek_unclaimed
			qui replace reverse_losstek_unclaimed=reverse_losshegtek_unclaimed
			qui replace nhood_tek_unclaimed=nhood_hegtek_unclaimed
			qui replace nhood_losstek_unclaimed=nhood_losshegtek_unclaimed
			
			foreach v in unclaimed claimed unclaimed_wt claimed_wt {
			
				qui replace thirdp_tek_`v'=thirdp_hegtek_`v'
				qui replace thirdp_losstek_`v'=thirdp_losshegtek_`v'

				}
				
			}
	
		* whenever losses in the indicator, restrict to cases pre-1st claim in dyad
		if "`indicator'"=="loss"|"`indicator'"=="losstek"| {
		
			qui drop if post_1st_claim==1
			
			}
	
		* include weighted vulnerability indicators
		local iv "`indicator'_unclaimed thirdp_`indicator'_unclaimed_wt thirdp_`indicator'_claimed_wt reverse_`indicator'_unclaimed reverse_claim_ongoing p_chal energy1 dem1_imp dem2_imp bothdem_imp ts1 ts2 ts3"
	
		* use regional fixed effects in full sample
		local regfe ""
		if `r'==0 {
		
			local regfe "asia mena europe"
			
			}
			
		* Estimates with DCRSEs
		dcr reg claim_onset nhood_`indicator'_unclaimed `iv' `regfe', dm1(chal) dm2(tgt) dofundo(asylike) psd dofcorr 
		
		mat b=e(b)
		mat V=e(V)
		local dof=e(N_unique_dm)-1
		
		* store results
		qui use results, clear
		qui replace reg=`r' in `i'
		qui replace bhat`k'=b[1,1] in `i'
		qui replace bhat`k'_up=b[1,1]+invttail(`dof',.025)*sqrt(V[1,1]) in `i'
		qui replace bhat`k'_lo=b[1,1]-invttail(`dof',.025)*sqrt(V[1,1]) in `i'
		qui save results, replace
	
		
		local k=`k'+1

	}

	
	local i=`i'+1
}


* graph results

* create variables for horizontal spacing
qui replace reg=reg-1 if reg>2
gen reg1=reg-.2
gen reg2=reg
gen reg3=reg+.2

* create Figure 4
twoway (scatter bhat1 reg1, msize(small)) (scatter bhat2 reg2, msize(small)) (scatter bhat3 reg3, msize(small))  ///
	(rcap bhat1_up bhat1_lo reg1) (rcap bhat2_up bhat2_lo reg2) (rcap bhat3_up bhat3_lo reg3), ///
	xtitle(" ") xscale(range(-.5 4.5)) ///
	xlabel(0 "All" 1 "Africa" 2 "Asia" 3 "MENA" 4 "Europe" 5 `" "Europe" "pre-47" "' 6 `" "Europe" "post-47" "') ///
	yline(0) ytitle("Marginal Effect") legend(order(1 2 3) cols(3) region(lstyle(none)))
	

*********************************************
* Results reported in Supplementary Appendix
*********************************************
	

************************************************************
* Appendix B - Validating the Indicators of Potential Caims
*************************************************************

use pandorasdata, clear

* swap in hegtek data for Europe

qui replace tek_unclaimed=hegtek_unclaimed if europe==1
qui replace losstek_unclaimed=losshegtek_unclaimed  if europe==1

foreach indicator in tek loss losstek {

		
	* whenever losses in the indicator, restrict to cases pre-1st claim in dyad
	if "`indicator'"=="loss"|"`indicator'"=="losstek" {
	
		qui drop if post_1st_claim==1
		
		}

	tab `indicator'_unclaimed claim_onset  if america==0, row chi
	
	}
		

**************************************************
* Appendix C - Create tables underlying Figure 3
**************************************************
	
* see above

*************************************************
* Appendix D  - Does Secession Risk Deter Claims?
*************************************************

* create file to store results
clear
set obs 6

forvalues i=1/3 {
	gen bhat`i'=. 
	gen bhat`i'_up=.
	gen bhat`i'_lo=.
	
	}

gen reg=.

label var bhat1 "Secessionist Movement"
label var bhat2 "Self-Determination Movement"
label var bhat3 "Proto-State"

save results, replace

* Go through regions

local i=1

foreach r in  1 3 4 5  {


	local k=1

	local indicator="tek"
	foreach secindicator in secmov1 sdm1 proto1 {

		use pandorasdata, clear
		qui keep if r`r'==1	

		local iv "`indicator'_unclaimed thirdp_`indicator'_unclaimed_wt thirdp_`indicator'_claimed_wt  reverse_`indicator'_unclaimed reverse_claim p_chal energy1 dem1_imp dem2_imp bothdem_imp ts1 ts2 ts3"
	
		dcr reg claim_onset `type'`secindicator' `iv', dm1(chal) dm2(tgt) dofundo(asylike) psd dofcorr 
		
		mat b=e(b)
		mat V=e(V)
		local dof=e(N_unique_dm)-1
		
		qui use results, clear
		qui replace reg=`r' in `i'
		qui replace bhat`k'=b[1,1] in `i'
		qui replace bhat`k'_up=b[1,1]+invttail(`dof',.025)*sqrt(V[1,1]) in `i'
		qui replace bhat`k'_lo=b[1,1]-invttail(`dof',.025)*sqrt(V[1,1]) in `i'
		qui save results, replace
	
		
		local k=`k'+1

	}

	
	local i=`i'+1
}


* fix spacing on horizontal axis
qui replace reg=reg-1 if reg>2
capture drop reg1-reg4
gen reg1=reg-.2
gen reg2=reg
gen reg3=reg+.2


* create Figure A1

twoway (scatter bhat1 reg1, msize(small)) (scatter bhat2 reg2, msize(small)) (scatter bhat3 reg3, msize(small))  ///
	(rcap bhat1_up bhat1_lo reg1) (rcap bhat2_up bhat2_lo reg2) (rcap bhat3_up bhat3_lo reg3), ///
	xtitle(" ") xscale(range(.5 4.5))	xlabel(1 "Africa" 2 "Asia" 3 "MENA" 4 "Europe") ///
	yline(0) ytitle("Marginal Effect") legend(order(1 2 3) cols(3) region(lstyle(none)))

	

****************************************************************
* Appendix E - The Failure of Mutual Restraint in South America
****************************************************************

use pandorasdata, clear
keep if samerica==1

* frequency of potential claims (restrict to first year of dyad)
egen minyear=min(year),by(dyad)
tab laclaim if minyear==year

* ever face a thirdparty claim
egen everthirdp_laclaim=max(thirdp_laclaim_unclaimed),by(ddyad)
gen everthirdp_laclaim_any = everthirdp_laclaim>0
tab everthirdp_laclaim_any if minyear==year

* rate of claims per directed dyad
egen everclaim_ddyad=max(claim_onset),by(ddyad)
tab everclaim_ddyad if minyear==year

* did potential claims predict actual claims
tab claim_onset laclaim if post_1st_claim==0, col chi

* Table A10
gen thirdp_laclaim_unclaimed_any=thirdp_laclaim_unclaimed>0&thirdp_laclaim_unclaimed!=.
tab  thirdp_laclaim_unclaimed_any claim_onset if laclaim==0&post_1st_claim==0, row chi
tab  thirdp_laclaim_unclaimed_any claim_onset if laclaim==1&post_1st_claim==0, row chi



*****************************************************
* Appendix F - The Effect of Potential Claim Cycles
****************************************************

* This code creates descriptive statistics on the frequency of claim cycles by region/subsample

foreach r in 0 1 3 4 5 6 7 {

	qui use pandorasdata, clear
	qui keep if r`r'==1&america==0

	* sub in hegtek data for europe
	
	if (`r'==0|`r'==5|`r'==6|`r'==7) {
	
		qui replace thirdp_tek_cycle=thirdp_hegtek_cycle	
			
		}
	
	qui gen thirdp_tek_cycle_any=thirdp_tek_cycle>0&thirdp_tek_cycle!=.
	egen evercycle=max(thirdp_tek_cycle_any),by(ddyad)
	

	if `r'==0 {	 
		local region "All"
		}
	if `r'==1 {
		local region "Africa"
		}
	if `r'==3 {
		local region "Asia"
		}
	if `r'==4 {
		local region "MENA"
		}
	if `r'==5 {
		local region "Europe"
		}
	if `r'==6 {
		local region "Europe Pre-1947"
		}
	if `r'==7 { 
		local region "Europe Post-1947"
		}
		
	
	di "`region'"
	summ thirdp_tek_cycle_any
	summ evercycle if year==bornyear
		
			
	}


* This code creates Figure A2

* create file to store results
clear
set obs 20

forvalues i=1/6 {
	qui gen bhat`i'=. 
	qui gen bhat`i'_up=.
	qui gen bhat`i'_lo=.
	
	}

gen reg=.
gen p1=.
gen p3=.

label var bhat1 "In Cycle (Ct)"
label var bhat2 "Not in Cycle (Ct)"
label var bhat3 "In Cycle (Wt)"
label var bhat4 "Not in Cycle (Wt)"

save results, replace

* Loop through regions

local i=1

foreach r in 0 1 3 4 5 6 7 {

local k=1

* loop through unweighted and weighted 

	foreach v in "" "_wt" {

		local indicator "thirdp_tek_cycle`v'"
		
		use pandorasdata, clear
		qui keep if r`r'==1&america==0

		*rescale dv
		replace claim_onset=claim_onset*100

		* sub in hegtek data for europe
	
		if (`r'==5|`r'==6|`r'==7) {
		
			qui replace tek=hegtek
			qui replace losstek=losshegtek
			qui replace tek_unclaimed=hegtek_unclaimed
			qui replace losstek_unclaimed=losshegtek_unclaimed
			qui replace reverse_tek_unclaimed=reverse_hegtek_unclaimed
			qui replace reverse_losstek_unclaimed=reverse_losshegtek_unclaimed
			qui replace thirdp_tek_cycle=thirdp_hegtek_cycle
			qui replace thirdp_tek_cycle_wt=thirdp_hegtek_cycle_wt		
			
			foreach v in unclaimed claimed unclaimed_wt claimed_wt {
			
				qui replace thirdp_tek_`v'=thirdp_hegtek_`v'
				qui replace thirdp_losstek_`v'=thirdp_losshegtek_`v'

				}
				
			}
	
		
		*baseline ivs
		local iv "tek_unclaimed reverse_tek_unclaimed reverse_claim_ongoing p_chal energy1 dem1_imp dem2_imp bothdem_imp ts1 ts2 ts3"

	
		qui gen thirdp_tek_nocycle`v'=thirdp_tek_unclaimed`v' - thirdp_tek_cycle`v'
	
		* ivs for test
		local iv3 "thirdp_tek_nocycle`v' thirdp_tek_claimed`v'"
	
		* use regional fixed effects in full sample
		local regfe ""
		if `r'==0 {
		
			local regfe "asia mena europe"
			
			}
			

		*estimate with DCRSEs
		dcr reg claim_onset `indicator' `iv3' `iv' `regfe', dm1(chal) dm2(tgt) dofundo(asylike) psd dofcorr 
		
		mat b=e(b)
		mat V1=e(V)
		mat V=e(V)
		local dof=e(N_unique_dm)-1
		
		
		* store results
		qui use results, clear
		
		* t-test for equal effects of thirdp_tek (in-cycle vs. not in cycle)
		qui test `indicator' = thirdp_tek_nocycle`v'
		qui replace p`k'=r(p) in `i'
		
		qui replace reg=`r' in `i'
		qui replace bhat`k'=b[1,1] in `i'
		qui replace bhat`k'_up=b[1,1]+invttail(`dof',.025)*sqrt(V1[1,1]) in `i'
		qui replace bhat`k'_lo=b[1,1]-invttail(`dof',.025)*sqrt(V1[1,1]) in `i'
				
		local k=`k'+1
	
		qui replace bhat`k'=b[1,2] in `i'
		qui replace bhat`k'_up=b[1,2]+invttail(`dof',.025)*sqrt(V1[2,2]) in `i'
		qui replace bhat`k'_lo=b[1,2]-invttail(`dof',.025)*sqrt(V1[2,2]) in `i'
		
		qui save results, replace
	
		
		local k=`k'+1

	
	}

		local i=`i'+1

}


* graph results*
use results, clear

* fix horizontal spacing
qui replace reg=reg-1 if reg>2
gen reg1=reg-.1
gen reg1star=reg
gen reg2=reg+.1
gen reg3=reg-.1
gen reg3star=reg
gen reg4=reg+.1

* create data to make significance stars
recode p1 0=.
recode p3 0=.
gen star1="*" if p1<0.05
gen star3="*" if p3<0.05

gen ystar1 = min(bhat1_lo,bhat2_lo)-.1 if p1!=.
gen ystar3 = min(bhat3_lo,bhat4_lo)-.1 if p3!=.

* Make figure
twoway (scatter bhat1 reg1, msize(small)  mcolor(black)) (scatter bhat2 reg2, msize(small) mcolor(black))  (scatter ystar1 reg1star, msymbol(none) mcolor(black) mlabel(star1) mlabtextstyle(heading) mlabpos(6))  ///
(rcap bhat1_up bhat1_lo reg1) (rcap bhat2_up bhat2_lo reg2)  ///
, xtitle(" ") xscale(range(-.5 4.5)) ///
xlabel(0 "All" 1 "Africa" 2 "Asia" 3 "MENA" 4 "Europe" 5 `" "Europe" "pre-47" "' 6 `" "Europe" "post-47" "', labsize(small)) ///
yline(0) ytitle("Marginal Effect") legend(order(1 2) cols(2) region(lstyle(none))) name(cyclecount, replace)

twoway (scatter bhat3 reg3, msize(small)  mcolor(black))  (scatter bhat4 reg4, msize(small)  mcolor(black)) (scatter ystar3 reg3star, msymbol(none) mcolor(black) mlabel(star3) mlabtextstyle(heading) mlabpos(6)) ///
(rcap bhat3_up bhat3_lo reg3) (rcap bhat4_up bhat4_lo reg4) ///
, xtitle(" ") xscale(range(-.5 4.5)) ///
xlabel(0 "All" 1 "Africa" 2 "Asia" 3 "MENA" 4 "Europe" 5 `" "Europe" "pre-47" "' 6 `" "Europe" "post-47" "', labsize(small)) ///
yline(0) ytitle("Marginal Effect") legend(order(1 2) cols(2) region(lstyle(none))) name(cycleweight, replace)
	
graph combine cyclecount cycleweight, cols(2)

	